<?php

 /*
  * This file is part of the People Of Walmart block for Moodle.
  * Copyright (C) 2009-2010 <anonymousmoodler@gmail.com>
  * 
  * The People Of Walmart block is distributed under the terms of the GNU
  * General Public License as published by the Free Software Foundation, either
  * version 3 of the License, or (at your option) any later version. You should
  * have received a copy of the GNU General Public License along with the People
  * Of Walmart block. If not, see <http://www.gnu.org/licenses/>.
  */

if(empty($_GET["img"]) || empty($_GET["w"]) || empty($_GET["h"]))
{
    exit();
}

require_once("../../config.php");

$img_url = urldecode($_GET["img"]);
$img_width = $_GET["w"];
$img_height = $_GET["h"];

$cache_dir = $CFG->dataroot."/cache/peopleofwalmart/images";
$cache_file = $cache_dir."/".sha1($img_url)."-".$img_width."x".$img_height.".jpg";

$thumbnail_quality = 75;

header("Content-type: image/jpeg");

if(!file_exists($cache_dir))
{
    mkdir($cache_dir);
}

if(file_exists($cache_file))
{
    $img = imagecreatefromjpeg($cache_file);
    imagejpeg($img, NULL, 100);
}
else
{
    list($img_full_width, $img_full_height) = getimagesize($img_url);
    $img = imagecreatetruecolor($img_width, $img_height);
    $img_full = imagecreatefromjpeg($img_url);
    imagecopyresampled($img, $img_full, 0, 0, 0, 0, $img_width, $img_height, $img_full_width, $img_full_height);
    imagejpeg($img, $cache_file, $thumbnail_quality);
    imagejpeg($img, NULL, $thumbnail_quality);
}

?>